package com.ruicar.afs.cloud.apply.business.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.apply.business.condition.TsysMessageCondition;
import com.ruicar.afs.cloud.apply.business.service.ApplyMessageNotifyService;
import com.ruicar.afs.cloud.apply.common.entity.TsysMessageNotify;
import com.ruicar.afs.cloud.apply.contract.enums.IsReadStatusEnum;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @Author Ice
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/message")
public class ApplyMessageNotifyController {

    private final ApplyMessageNotifyService applyMessageNotifyService;

    @PostMapping("/save")
    @ApiOperation("业务选择配置-根据id获取信息")
    public IResponse<List<TsysMessageNotify>> getInfo(@RequestBody List<TsysMessageNotify> condition) {
        return IResponse.success(applyMessageNotifyService.saveBatch(condition));
    }

    @PostMapping("/saveMessage")
    @ApiOperation("业务选择配置-根据id获取信息")
    public IResponse<TsysMessageNotify> saveMessage(@RequestBody TsysMessageNotify condition) {
        return IResponse.success(applyMessageNotifyService.save(condition));
    }

    @PostMapping("/getInfo")
    @ApiOperation("业务选择配置-根据id获取信息查询未读状态消息通知")
    public IResponse<IPage<TsysMessageNotify>> getInfo(@RequestBody TsysMessageCondition condition) {
        Page page = new Page(condition.getPageNumber(), condition.getPageSize());
        IPage<TsysMessageNotify> list=applyMessageNotifyService.page(page,Wrappers.<TsysMessageNotify>query().lambda()
                .eq(StringUtils.isNotEmpty(condition.getStatus()),TsysMessageNotify::getStatus,condition.getStatus())
                .eq(TsysMessageNotify::getChannelId,condition.getChannelId()));
        return IResponse.success(list);
    }

    @PostMapping("/updateInfo")
    @ApiOperation("业务选择配置-根据id更新消息通知未读状态为已读")
    public IResponse<List<TsysMessageNotify>> updateInfo(@RequestBody TsysMessageNotify condition) {
        TsysMessageNotify tsysMessageNotify = applyMessageNotifyService.getOne(Wrappers.<TsysMessageNotify>query().lambda().eq(TsysMessageNotify::getId,condition.getId()));
        if(tsysMessageNotify!=null){
            tsysMessageNotify.setStatus(IsReadStatusEnum.READ.getIndex());
            applyMessageNotifyService.updateById(tsysMessageNotify);
        }
        return IResponse.success(Boolean.TRUE);
    }

}
